package com.googlecode.connectlet.crypto.cipher.mode;

import com.googlecode.connectlet.crypto.cipher.Decipheror;
import com.googlecode.connectlet.util.Bytes;

public class CbcNoPaddingDecipheror extends EcbNoPaddingDecipheror implements CbcCipher {
	protected byte[] chain;

	public CbcNoPaddingDecipheror(Decipheror cipher) {
		super(cipher);
		chain = new byte[blockSize];
	}

	public CbcNoPaddingDecipheror(Decipheror cipher, byte[] iv) {
		this(cipher);
		init(iv);
	}

	@Override
	public void init(byte[] input) {
		init();
		System.arraycopy(input, 0, chain, 0, blockSize);
	}

	@Override
	protected void decryptNext(byte[] output, int outputOffset) {
		cipher.decrypt(block, 0, output, outputOffset);
		Bytes.xor(chain, 0, output, outputOffset, blockSize);
		System.arraycopy(block, 0, chain, 0, blockSize);
	}
}